if (!window.console) console = {log:function () {} };

var PITCHWISE = PITCHWISE || {};
PITCHWISE.login = PITCHWISE.login || {};

PITCHWISE.login = function (options) {
    'use strict';

    this.authenticated = options.authenticated;
    this.role = options.role;
    this.logoutUrl = options.logoutUrl;
    this.el = {
        'loginForm':$('#loginform'),
        'login':$('#menu-item-98'),
        'logout':$('#menu-item-164'),
        'register':$('#menu-item-26'),
        'myAccount':$('#menu-item-71'),
        'myVenue':$('#menu-item-105'),
        'loginModal':$('#loginModal'),
        'menu':$('#menu')
    };

    console.log('PITCHWISE.login.options', options);
    this.load();
};
PITCHWISE.login.prototype.events = function () {
    var _this = this;

    _this.el.login.on('click', function (e) {
        _this.el.loginModal.modal({'backdrop':'static'});
        return false;
    });

    _this.el.loginModal.on('shown', function () {
        $('#user_login').focus();
    })

    _this.el.loginForm.on('submit', function (e) {
        $('#login-alert').stop(true, false).slideUp();
        if ($('#user_login').val() == "" || $('#user_pass').val() == "") {
            $('#login-alert').text("Please enter a username and password.").stop(true, false).slideDown();
        } else {
            var input_data = $(this).serialize();
            $.ajax({
                type:"POST",
                url:"/wp-login.php",
                data:input_data,
                success:function (msg) {
                    var reg1 = /login_error/g;
                    if (reg1.test(msg)) {
                        $('#login-alert').text("Your login credentials are incorrect. Please try again.").stop(true, false).slideDown();
                        console.log("Your login credentials are incorrect. Please try again.");
                        //console.log(msg);
                    }
                    else {
                        //console.log(msg);
                        location.reload();
                    }
                }
            });
        }
        return false;
    });


};
PITCHWISE.login.prototype.init = function () {
    this.security();
    this.el.menu.fadeIn(1500);
};
PITCHWISE.login.prototype.security = function () {
    var _this = this;

    this.el.login.css({'display':'none'});
    this.el.register.css({'display':'none'});
    this.el.myAccount.css({'display':'none'});
    this.el.myVenue.css({'display':'none'});
    this.el.logout.css({'display':'none'});

    if (_this.authenticated) {
        this.el.logout.css({'display':'inline-block'});
        this.el.logout.find('a').attr('href', _this.logoutUrl.replace(/amp;/g, ""));
        if (_this.role == 'operator' || _this.role == 'administrator') {
            this.el.myVenue.css({'display':'inline-block'});
        }
        if (_this.role == 'customer' || _this.role == 'administrator') {
            this.el.myAccount.css({'display':'inline-block'});
        }
    } else {
        this.el.login.css({'display':'inline-block'});
        this.el.register.css({'display':'inline-block'});
    }
    ;
};
PITCHWISE.login.prototype.load = function () {
    this.events();
    this.init();
};